package com.school.food.controller;

import com.school.food.domain.Collection;
import com.school.food.service.CollectionService;
import com.school.support.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.school.support.annocation.request.LoginInfo;
import com.school.support.response.Response;

import java.util.Date;

/**
 * Created by 执笔 on 2017/3/19.
 */
@Controller
@RequestMapping("collection")
@LoginInfo(true)
public class CollectionController extends BaseController {

    @Autowired
    private CollectionService collectionService;

    @RequestMapping("collect")
    @ResponseBody
    public Response<String> collect(Integer foodId){
        Collection collection = new Collection();
        collection.setFoodId(foodId);
        collection.setUserId(sessionUser().getId());
        if(null == collectionService.selectOne(collection)){
            collection.setAddtime(new Date());
            collectionService.insertSelective(collection);
            return Response.ok("1");
        }else{
            collectionService.delete(collection);
            return Response.ok("2");
        }
    }
}
